﻿// This code is released under the BSD license.
namespace PugLib
{
    using System.Text.RegularExpressions;

    public static class RegexUtils
    {
        public const string RegexEmail = @"([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,8}|[0-9]{1,8})(\]?)";

        public const string RegexIPAddress =
            @"(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)";

        //Matches a positive or negative decimal value with any precision and scale (whole number or decimal). 
        //Allows for left-padded zeroes, commas as group separator, negative sign (-) or parenthesis to indicate negative number.
        public const string RegexNumeric = @"^\-?\(?([0-9]{0,3}(\,?[0-9]{3})*(\.?[0-9]*))\)?$";

        public const string RegexUrl = @"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*";


        //wraps the default regex isMatch to include regex options to pre compile and ignore case. also checks for nulls and empty strings
        public static bool IsExactMatch(string input, string pattern)
        {
            if (StringUtilities.IsEmpty(input))
            {
                return false;
            }
            Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (!m.Success)
            {
                return false;
            }
            return m.Groups[0].Value == input;
        }
    }
}